package com.ruoyi.web.controller.city.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.controller.city.domain.CmsCity;
import com.ruoyi.web.controller.city.service.ICmsCityService;


/**
 * 城市Controller
 *
 * @author ruoyi
 * @date 2020-10-09
 */
@RestController
@RequestMapping("/city")
public class CmsCityController extends BaseController
{
    @Autowired
    private ICmsCityService cmsCityService;

    /**
     * 查询城市列表
     */
//    @PreAuthorize("@ss.hasPermi('system:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsCity cmsCity)
    {
        startPage();
        List<CmsCity> list = cmsCityService.selectCmsCityList(cmsCity);
        return getDataTable(list);
    }

    /**
     * 导出城市列表
     */
//    @PreAuthorize("@ss.hasPermi('system:city:export')")
//    @Log(title = "城市", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CmsCity cmsCity) throws IOException
//    {
//        List<CmsCity> list = cmsCityService.selectCmsCityList(cmsCity);
//        ExcelUtil<CmsCity> util = new ExcelUtil<CmsCity>(CmsCity.class);
//        util.exportExcel(response, list, "city");
//    }

    /**
     * 获取城市详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:city:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsCityService.selectCmsCityById(id));
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(CmsCity city)
    {
        city.setDisabled(0);
        List<CmsCity> citys = cmsCityService.selectCmsCityList(city);
        return AjaxResult.success(cmsCityService.buildCityTreeSelect(citys));
    }

    /**
     * 新增城市
     */
//    @PreAuthorize("@ss.hasPermi('system:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsCity cmsCity)
    {
        cmsCity.setCreateAt(DateUtils.getNowDate());
        return toAjax(cmsCityService.insertCmsCity(cmsCity));
    }

    /**
     * 修改城市
     */
//    @PreAuthorize("@ss.hasPermi('system:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsCity cmsCity)
    {
        return toAjax(cmsCityService.updateCmsCity(cmsCity));
    }

    /**
     * 删除城市
     */
//    @PreAuthorize("@ss.hasPermi('system:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsCityService.deleteCmsCityByIds(ids));
    }
}